//
//  LSYMainTabBar.swift
//  LSYWeibo
//
//  Created by 刘诗岩 on 16/4/6.
//  Copyright © 2016年 刘诗岩. All rights reserved.
//

import UIKit

class LSYMainTabBar: UITabBar {
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(plusButton)
    }

    // 通过 xib / storyboard 创建时会调用这个方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let itemW = self.frame.width / 5
        let itemH = self.frame.height
        
        // 设置 tabBarItem
        let rect = CGRect(x: 0, y: 0, width: itemW, height: itemH)
        
        // 创建一个序号,用于布局,强转为 CGFloat 格式
        var index : CGFloat = 0
        
        for subview in subviews{
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                subview.frame = CGRectOffset(rect, itemW * index, 0)
                
                index += (index == 1) ? 2 : 1
            }
            
            
        }
        
        // frame 值有一点不对
        // 按钮的实际边距大于显示边距,又被上一个 item 覆盖了边角.
        plusButton.frame = CGRectOffset(rect, 2 * itemW, 0)
        
        bringSubviewToFront(plusButton)
    }
    
    // MARK: - 懒加载
    lazy var plusButton : UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        return button
    }()

}
